//
//  HWCollectionViewFlowLayout.swift
//  HWBanner
//
//  Created by fuyoufang on 2021/10/14.
//

import Foundation
import UIKit

class HWCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
    // MARK: Constant
    struct Constant {
        static let space: CGFloat = 0
        static let activeDistance: CGFloat = 400 //垂直缩放除以系数
        static let scaleFactor: CGFloat = 0.25 //缩放系数  越大缩放越大
    }
    
    var scrollViewAnimator: ScrollViewAnimator?
    
    override func prepare() {
        super.prepare()
        scrollDirection = .horizontal
        collectionView?.isPagingEnabled = true
        minimumLineSpacing = Constant.space
        minimumInteritemSpacing = Constant.space
        sectionInset = UIEdgeInsets(top: 0, left: Constant.space, bottom: 0, right: Constant.space)
        if let collectionView = collectionView {
            let size = collectionView.frame.size
            itemSize = CGSize(width: size.width - Constant.space * 2,
                              height: size.height)
            
        }
        
        if let collectionView = collectionView {
            let scrollViewAnimator = ScrollViewAnimator(scrollView: collectionView, timingFunction: .circleIn)
            self.scrollViewAnimator = scrollViewAnimator
        }
        startTimer()
    }
    
    var timer: Timer?
    
    func startTimer() {
        if self.timer == nil {
            let timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(scrollToNext), userInfo: nil, repeats: true)
            self.timer = timer            
        }
    }
    
    @objc func scrollToNext() {
        guard let collectionView = collectionView,
              !collectionView.isDragging else {
            return
        }
        var next = collectionView.contentOffset
        next.x += collectionView.bounds.size.width
        
        if next.x < (collectionView.contentSize.width - collectionView.bounds.size.width) {
            
        } else {
            next = .zero
        }
        debugPrint("next:\(next)")
        scrollViewAnimator?.setContentOffset(next, duration: 1)
    }
    
    override class var layoutAttributesClass: AnyClass {
        return HWLayoutAttributes.self
    }
    
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        guard let array = super.layoutAttributesForElements(in: rect) as? [HWLayoutAttributes] else {
            return nil
        }
        var visibleRect = CGRect()
        visibleRect.origin = self.collectionView!.contentOffset
        visibleRect.size = self.collectionView!.bounds.size
        for attributes in array {
            let offset = (attributes.frame.minX - self.collectionView!.contentOffset.x)
            if offset <= 0 {
                attributes.bannerSate = .none
            } else {
                var progress = (offset) / attributes.bounds.size.width
                progress = progress / 2
                progress = min(1, progress)
                //debugPrint("offset:\(offset), width:\(attributes.bounds.size.width) progress:\(progress)")
                attributes.bannerSate = .animationStage(progress: progress)
            }

            let distance = visibleRect.midX - attributes.center.x
            let normalizedDistance = abs(distance / Constant.activeDistance)
            let zoom = 1 - Constant.scaleFactor * normalizedDistance
            attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0)
        }
        return array
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
}
